package com.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.common.Result;
import com.example.dto.PurQueryDto;
import com.example.entity.*;
import com.example.service.AccountService;
import com.example.service.HnyyPurService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService infoService;
    @Autowired
    private HnyyPurService hnyyPurService;
    @PostMapping("/getData")
    public Result getData(@RequestBody VHnyyPurRebateSaleList vHnyyPurRebateSaleList, HttpServletRequest request) {
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<VHnyyPurRebateSaleList> info = infoService.getData(vHnyyPurRebateSaleList, comPartyId);
        return Result.success(info);
    }
    @GetMapping("/updateAmt")
    public Result updateAmt(@RequestParam String id, @RequestParam String rebAmt, HttpServletRequest request) {
        String comPartyId = (String) request.getAttribute("comPartyId");
        String saler=(String) request.getAttribute("saler");
        int i = infoService.updateAmt(id, rebAmt, comPartyId, saler);
        return i > 0 ? Result.ok() : Result.error();

    }
    @GetMapping("/apply")
    public Result apply(@RequestParam String applyAmt, @RequestParam String Id,@RequestParam String venderId,String purNo,HttpServletRequest request){
        String comPartyId = (String) request.getAttribute("comPartyId");
        String saler=(String) request.getAttribute("saler");
        Long i=infoService.apply(comPartyId,saler,venderId,purNo,Id,applyAmt);
        return Result.success(i);
    }
    @PostMapping("/applyHx")
    public Result applyHx(@RequestBody HnyyPurRebateSaleHxLines hxLines, HttpServletRequest request){
        String comPartyId = (String) request.getAttribute("comPartyId");
        System.out.println("hxLines=========>"+JSONObject.toJSONString(hxLines));
        hxLines.setInvOwner(comPartyId);
        int i=infoService.applyHx(hxLines);
        return i>0?Result.ok():Result.error();
    }

    @GetMapping("/getHxList")
    public Result getHxList(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam String startDate,
                            @RequestParam String endDate,
                            @RequestParam String key,
                            @RequestParam (required = false) String venderId,
                            @RequestParam (required = false) String creater, HttpServletRequest request){
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<HnyyPurRebateSaleHx> info = infoService.getHxList(pageNum, pageSize, creater,venderId,startDate,endDate,comPartyId,key);
        return Result.success(info);
    }

    @GetMapping("/getHxListById")
    public Result getHxListById(@RequestParam String id){
        VHnyyPurRebateSaleList info = infoService.getHxListById(id);
        return Result.success(info);
    }

    @GetMapping("/saleCheck")
    public Result saleCheck(@RequestParam String id,@RequestParam String memo,String key, HttpServletRequest request){
        String saler=(String) request.getAttribute("saler");
        int i=infoService.saleCheck(id,memo,key,saler);
        return i>0?Result.ok():Result.error();
    }

    @PostMapping("/getPurData")
    public Result getPurData(@RequestBody PurQueryDto purQueryDto, HttpServletRequest request) {
        String comPartyId = (String) request.getAttribute("comPartyId");
        purQueryDto.setInvOwner(comPartyId);
        PageInfo<PurQueryDto> info = hnyyPurService.getData(purQueryDto);
        return Result.success(info);
    }
    @PostMapping("/updatePurAmt")
    public Result updatePurAmt(@RequestBody List<PurQueryDto> purQueryDtoList, HttpServletRequest request) {
        return hnyyPurService.updatePurAmt(purQueryDtoList);

    }

    @PostMapping({"/applyPur"})
    public Result applyPur(@RequestBody List<HnyyPurRebatePurHxLines> hnyyPurRebatePurHxLines, HttpServletRequest request) {
        String comPartyId = (String)request.getAttribute("comPartyId");
        String user = (String)request.getAttribute("userId");
        return hnyyPurService.applyPur(comPartyId, user, hnyyPurRebatePurHxLines);

    }

    @PostMapping({"/getPurCGCheck"})
    public Result getPurCGCheck(@RequestBody HnyyPurRebatePurHx purRebatePurHx, HttpServletRequest request) {
        String comPartyId = (String)request.getAttribute("comPartyId");
        purRebatePurHx.setInvOwner(BigDecimal.valueOf(Long.valueOf(comPartyId)));
        PageInfo<HnyyPurRebatePurHx> info  = hnyyPurService.getPurCGCheck(purRebatePurHx);
        return Result.success(info);
    }

    @GetMapping("/getPurHxLines")
    public Result getPurHxLines(@RequestParam BigDecimal hnyyPurRebatePurHxId,@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        PageInfo<HnyyPurRebatePurHxLines> data = hnyyPurService.getHnyyPurRebatePurHxLines(hnyyPurRebatePurHxId,pageNum,pageSize);
        return Result.success(data);
    }

    @PostMapping({"/purBuyerPass"})
    public Result purBuyerPass(@RequestBody List<HnyyPurRebatePurHx> purRebatePurHxes, HttpServletRequest request) {
        String user = (String)request.getAttribute("userId");
        BigDecimal userId = BigDecimal.valueOf(Long.valueOf(user));
        for(HnyyPurRebatePurHx h : purRebatePurHxes){
            h.setBuyer(userId);
            h.setOpFlag("ACCEPT");
        }
        return hnyyPurService.buyerPass(purRebatePurHxes);
    }
    @PostMapping({"/purCwPass"})
    public Result purCwPass(@RequestBody List<HnyyPurRebatePurHx> purRebatePurHxes, HttpServletRequest request) {
        String user = (String)request.getAttribute("userId");
        BigDecimal userId = BigDecimal.valueOf(Long.valueOf(user));
        for(HnyyPurRebatePurHx h : purRebatePurHxes){
            h.setBuyer(userId);
            h.setOpFlag("FINISH");
        }
        return hnyyPurService.buyerPass(purRebatePurHxes);
    }


}
